package MypageController.MyKategore;

import java.io.IOException;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.PagingManager;

import MypageController.MyComment.MyCommentBean;
import MypageController.MyMain.MyMainBean;
import MypageController.MyMain.MyMainDAO;
import MypageController.MyWrite.MyWriteBean;

/**
 * Servlet implementation class MyKategoreServ
 */
public class MyKategoreServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyKategoreServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=UTF-8");
 		RequestDispatcher view = null;
 		MyWriteBean[] mywritebean = null;
 		MyCommentBean[] mycommentbean = null;
 		MyKategoreBean[] kategorilist =null;
 		MyKategoreDAO mykategoredao = new MyKategoreDAO();
 		MyMainDAO mymaindao = new MyMainDAO();
 		MyMainBean mymainbean = new MyMainBean();
 		String action=request.getParameter("action");
 		String id=null;
 		if(request.getParameter("id") != null){
 			id=request.getParameter("id");
 		}
 		String url = null;
 		
 		if(action.equals("kategorie")){
 			
 			String kategore = request.getParameter("kategore");
 			int mypageid = Integer.parseInt(request.getParameter("mypageid"));
 			int count = mykategoredao.kategoreEntriesCount(mypageid, kategore);
 			int level = mymaindao.level(id);
 			mycommentbean = mymaindao.latestComment(mypageid);
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mymainbean = mymaindao.myPageDataGet(mypageid);
System.out.println("카테고리수 = " + count); 			
 			if(count != 0){
	 			int currentPage = 1;
	 			if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
	 		   	
	 		   	HashMap param=new HashMap();
				param = new HashMap<String, Object>();
				param.clear();
				
				param.put("pageSize", 7);
				param.put("currentPage", currentPage );
				param.put("pageGroupSize", 5 );
				param.put("totalCount", count);
	
				PagingManager paging = new common.PagingManager();
				paging.setPageNo(param);

	 			mywritebean = mykategoredao.kategoreEntriesGet(mypageid, kategore, currentPage, 7);
	
				String pagename = "/Mypage/Mp_KateView";
				request.setAttribute("page", pagename);
				request.setAttribute("id", id);
				request.setAttribute("mypagedata", mymainbean);
				request.setAttribute("mypage", "mypage");
				request.setAttribute("divide", "kategorie");
				request.setAttribute("paging", paging.getReturnHtml());
				request.setAttribute("level", level);
				request.setAttribute("mypageid", mypageid);
				request.setAttribute("entries", mywritebean);
				request.setAttribute("kategorilist", kategorilist);
				request.setAttribute("kategdate", kategdate);
				request.setAttribute("latestcomment", mycommentbean);
				
				if(level == 1){
					url="/Main/OldMain/Om_Main.jsp";
				}else{
					url="/Main/YoungMain/Ym_Main.jsp";
				}
		
				view=request.getRequestDispatcher(url);
				view.forward(request,response); 
 			}else{

/**************************************************************************************************/
 	
 	 			mypageid = Integer.parseInt(request.getParameter("mypageid"));
 	 			
 	 			int currentPage = 1;
 	 			if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
 					currentPage=Integer.parseInt(request.getParameter("currentPage"));
 				}
 	 			if(currentPage == 0){
 	 				currentPage = 1;
 	 			}

 	 		   	HashMap param=new HashMap();
 				param = new HashMap<String, Object>();
 				param.clear();
 				
 				param.put("pageSize", 7);
 				param.put("currentPage", currentPage );
 				param.put("pageGroupSize", 5 );
 				count = mymaindao.entriesCount(mypageid);
 				param.put("totalCount", count);

 				PagingManager paging = new common.PagingManager();
 				paging.setPageNo(param);
 					
 				Integer[] entriesid = mymaindao.entriesIdGet(mypageid);
 				
 				for(int i = 0; i<entriesid.length;i++){		
 					mymaindao.commentCountSet(entriesid[i]);
 					mymaindao.entriesNumberSet(entriesid[i], i);
 				}
 				mywritebean = mymaindao.entriesGet(mypageid, currentPage, 7);
 				
 				mycommentbean = mymaindao.latestComment(mypageid);
 				for(int i=0;i<mycommentbean.length;i++){						
 					int entriesnum = mymaindao.entriesNumberGet(mycommentbean[i].getEntries_Id());
 					mycommentbean[i].setEntries_Number(entriesnum);
 				}
 					
 				String pagename = "/Mypage/Mp_Bord";
 				request.setAttribute("paging", paging.getReturnHtml());
 				request.setAttribute("page", pagename);
 				request.setAttribute("currentPage", currentPage);
 				request.setAttribute("id", id);
 				request.setAttribute("mypagedata", mymainbean);
 				request.setAttribute("mypage", "mypage");
 				request.setAttribute("level", level);
 				request.setAttribute("mypageid", mypageid);
 				request.setAttribute("entries", mywritebean);
 				request.setAttribute("kategorilist", kategorilist);
 				request.setAttribute("kategdate", kategdate);
 				request.setAttribute("latestcomment", mycommentbean);
 				if(level == 1){
 					url="/Main/OldMain/Om_Main.jsp";
 				}else{
 					url="/Main/YoungMain/Ym_Main.jsp";
 				}
 		
 				view=request.getRequestDispatcher(url);
 				view.forward(request,response); 
 				
 /***************************************************************************************************/				
 			}
 		}else if(action.equals("kategoriDate")){
 			
 			Date kategoredate = Date.valueOf(request.getParameter("kategoredate"));
 			int mypageid = Integer.parseInt(request.getParameter("mypageid"));
 			
			int currentPage = 1;
 			if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
 		   	
 		   	HashMap param=new HashMap();
			param = new HashMap<String, Object>();
			param.clear();
			
			param.put("pageSize", 7);
			param.put("currentPage", currentPage );
			param.put("pageGroupSize", 5 );
			int count = mykategoredao.kateDateEntriesCount(mypageid, kategoredate);
			param.put("totalCount", count);

			PagingManager paging = new common.PagingManager();
			paging.setPageNo(param);
 			
 			int level = mymaindao.level(id);
 			
 			mywritebean = mykategoredao.kateDateEntriesGet(mypageid, kategoredate, currentPage, 7);
 			
 			mycommentbean = mymaindao.latestComment(mypageid);
 			kategorilist = mymaindao.kategoriesGet(mypageid);
			for(int i = 0; i<kategorilist.length; i++){
				kategorilist[i].setKate_Count(mymaindao.kategorisCountGet(mypageid, kategorilist[i].getKeta_Title()));
			}
 			String[] kategdate = mymaindao.kategoriesDateGet(mypageid);
 			mymainbean = mymaindao.myPageDataGet(mypageid);
 			
			String pagename = "/Mypage/Mp_KateView";
			request.setAttribute("page", pagename);
			request.setAttribute("id", id);
			request.setAttribute("mypagedata", mymainbean);
			request.setAttribute("mypage", "mypage");
			request.setAttribute("divide", "kategoriDate");
			request.setAttribute("paging", paging.getReturnHtml());
			request.setAttribute("level", level);
			request.setAttribute("mypageid", mypageid);
			request.setAttribute("entries", mywritebean);
			request.setAttribute("kategorilist", kategorilist);
			request.setAttribute("kategdate", kategdate);
			request.setAttribute("latestcomment", mycommentbean);
			
			if(level == 1){
				url="/Main/OldMain/Om_Main.jsp";
			}else{
				url="/Main/YoungMain/Ym_Main.jsp";
			}
	
			view=request.getRequestDispatcher(url);
			view.forward(request,response); 
 			
 		}else if(action.equals("kategoriedelet")){
 			
 			String category = request.getParameter("kategorie");
 			int mypageid = Integer.parseInt(request.getParameter("mypageid"));
 			
 			Integer[] kateentriesid = mykategoredao.kategoreEntriesIdGet(mypageid, category);
 			for(int i=0;i<kateentriesid.length;i++){
 				mykategoredao.commentDelet(mypageid, kateentriesid[i]);
 			}
 			
 			mykategoredao.kategoreDelet(mypageid, category);
 			
 		}else if(action.equals("kategorieadd")){
 			
 			String category = request.getParameter("kategorie");
 			int mypageid = Integer.parseInt(request.getParameter("mypageid"));
 			
 			mykategoredao.kategoreAdd(mypageid, category);
 			
 		}
 		
	}

}
